/*
 * TabControl.cpp
 *
 *  Created on: 2011-1-16
 *      Author: Administrator
 */

#include "TabControl.h"

CTabControl::CTabControl()
	{
	iTabCaptions =  new (ELeave) CDesC16ArrayFlat(5);
	iIndex = 0;
	}

CTabControl::~CTabControl()
	{
	iTabCaptions->Reset();
	delete iTabCaptions;
	iTabCaptions = NULL;
	iRects.Reset();
	CImageFactory::getInstance()->ReleaseImage(iFocusImage);
	CImageFactory::getInstance()->ReleaseImage(iLostFocusImage);
	}

CTabControl* CTabControl::NewL(const TRect& aRect,const CCoeControl* aParent)
	{
	CTabControl* self = CTabControl::NewLC(aRect,aParent);
    CleanupStack::Pop(self);
    return self;
	}

CTabControl* CTabControl::NewLC(const TRect& aRect,const CCoeControl* aParent)
	{
	CTabControl* self = new(ELeave) CTabControl();
    CleanupStack::PushL(self);
    self->ConstructL(aRect,aParent);
    return self;
	}

void CTabControl::ConstructL(const TRect& aRect,const CCoeControl* aParent)
	{
    if(aParent == NULL)
        {
        CreateWindowL();
        }
    else
        {
        SetContainerWindowL(*aParent);
        }
    SetRect(aRect);
    ActivateL();
	}

void CTabControl::Draw(CWindowGc& gc, const TRect& aRect) const
	{
	CBaseControl::Draw(gc,aRect);
	gc.UseFont(Font());
	gc.SetPenColor(KRgbWhite);
	TInt baseline = 0;
	TRect rect;
	for (TInt i = 0; i < iTabCaptions->Count(); i++)
		{
		rect = iRects[i];
		baseline = rect.Height() / 2 + Font()->AscentInPixels() / 2;
		rect.Move(Position());
		
		if (i == iIndex)
			{
			CGraphicsTool::DrawImage(gc, iFocusImage->Bitmap(),iFocusImage->MaskBitmap(), rect);
			}
		else
			{
			CGraphicsTool::DrawImage(gc, iLostFocusImage->Bitmap(),iLostFocusImage->MaskBitmap(), rect);
			}
		gc.DrawText(iTabCaptions->MdcaPoint(i), rect, baseline, CGraphicsContext::ECenter, 0);
		}
	rect = Rect();
	rect.iTl.iY = Rect().iBr.iY - 4;
	rect.SetHeight(4);
	gc.SetBrushColor(TRgb(181,5,16));
	gc.SetBrushStyle(CGraphicsContext::ESolidBrush);
	gc.SetPenStyle(CGraphicsContext::ENullPen);
	gc.DrawRect(rect);
	
	gc.DiscardFont();
	}

TKeyResponse CTabControl::OfferKeyEventL( 
			const TKeyEvent& aKeyEvent, 
			TEventCode aType )
	{
    if (aType == EEventKey)
        {
        switch (aKeyEvent.iCode)
            {
            case EKeyLeftArrow:
                if (iIndex > 0)
                	setIndex(iIndex - 1);
                return EKeyWasConsumed;
            case EKeyRightArrow:
                if (iIndex < (iTabCaptions->Count()-1))
                	setIndex(iIndex + 1);
                return EKeyWasConsumed;
            }
        }
    return EKeyWasNotConsumed;
	}

void CTabControl::HandlePointerEventL(const TPointerEvent& aPointerEvent)
	{
	TRect rect;
    switch (aPointerEvent.iType)
       {
		   case TPointerEvent::EButton1Up:
		   case TPointerEvent::EButton2Up:
		   case TPointerEvent::EButton3Up:
			   for (TInt i = 0; i < iRects.Count(); i++)
				   {
				   rect = iRects[i];
				   rect.Move(Position());
				   if (rect.Contains(aPointerEvent.iPosition))
					   {
					   setIndex(i);
					   break;
					   }
				   }
			   break;
       }
	}

void CTabControl::SetLayout()
	{
	TInt width = 0;
	TInt beginX = 10;
	for (TInt i = 0; i < iTabCaptions->Count(); i++)
		{
		width = Font()->TextWidthInPixels(iTabCaptions->MdcaPoint(i));
		if (iFocusImage)
			width = iFocusImage->SizeInPixels().iWidth;
		if (width < 80)
			width = 80;
		TRect rect = Rect();
		rect.SetHeight(Rect().Height());
		rect.iTl.iX = beginX;
		rect.iBr.iX = beginX + width;
		beginX += width;
		beginX += 10;
		iRects.AppendL(rect);
		}
	}

void CTabControl::AddTabCaptionL(const TDesC& aCaption)
	{
	iTabCaptions->AppendL(aCaption);
	}

void CTabControl::setIndex(TInt aIndex)
	{
	if (aIndex >= 0 && aIndex < iTabCaptions->Count())
		{
		if (iIndex != aIndex)
			{
			iIndex = aIndex;
			if (iControlEventObserver)
				iControlEventObserver->HandleControlEventL(this,EClick);
			}
		}
	}

TInt CTabControl::Index()
	{
	return iIndex;
	}

void CTabControl::setFocusImageL(CImage* aImage)
	{
	//delete iFocusImage;
	CImageFactory::getInstance()->ReleaseImage(iFocusImage);
	iFocusImage = aImage;
	}

void CTabControl::setLostFocusImageL(CImage* aImage)
	{
	//delete iLostFocusImage;
	CImageFactory::getInstance()->ReleaseImage(iLostFocusImage);
	iLostFocusImage = aImage;
	}
